//
// Created by lenovo on 24-8-10.
//

#include "game.h"

void init_board(char board[ROW][COL],int row,int col)
{
    int i = 0;
    for(i=0;i<row;i++)
    {
        int j = 0;
        for (j=0;j<col;j++)
        {
            board[i][j]=' ';
        }
    }
}

void print_board(char board[ROW][COL],int row,int col)
{
    int i = 0;
    for(i=0;i<row;i++)//every row
    {
        int j = 0;
        for(j=0;j<col;j++)//printf("   |   |   \n");
        {
            printf(" %c ",board[i][j]);//先打印棋盘中字符
            if(j<col-1)
                printf("|");//后打印分隔的'|',直到最后一个字符打印完就不用打印分隔符了
        }
        printf("\n");//打印完一行，然后换行
        if(i<row-1)//printf("---|---|---\n");
        {
            for(j=0;j<col;j++)
            {
                printf("---");
                if(j<col-1)
                    printf("|");
            }
            printf("\n");
        }
    }
}

void player_move(char board[ROW][COL],int row,int col)
{
    printf("player move\n");
    while(1)
    {
        printf("please input the coordinate:");
        int x = 0;
        int y = 0;
        scanf("%d %d",&x,&y);
        if(x>=1 && x<=row && y>=1 && y<=col)
        {
            if(board[x-1][y-1]==' ')
            {
                board[x-1][y-1]='*';
                break;
            }
            else
                printf("the coordinate are already occupied,plase input again\n");
        }
        else
        {
            printf("coordinate error\n");
        }
    }
}

void computer_move(char board[ROW][COL],int row,int col)
{
    printf("computer move\n");
    while(1)
    {
        int x = rand()%row;
        int y = rand()%col;

        if(board[x][y]==' ')
        {
            board[x][y]='#';
            break;
        }
    }
}

int is_full(char board[ROW][COL],int row,int col)
{
    int i = 0;
    int j = 0;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            if (board[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}

char is_win(char board[ROW][COL],int row,int col)
{
    int i = 0;
    for(i=0;i<row;i++)//判断三行
    {
        if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0]!=' ')
            return board[i][0];
    }
    int j = 0;
    for (j=0;j<col;j++)//判断三列
    {
        if(board[0][j]==board[1][j] && board[1][j]==board[2][j] && board[0][j]!=' ')
            return board[0][j];
    }
    //对角线
    if (board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[1][1]!= ' ')
    {
        return board[1][1];
    }
    if (board[0][2]==board[1][1] && board[1][1]==board[2][0] && board[1][1]!= ' ')
    {
        return board[1][1];
    }
    //平局
    if(is_full(board,row,col)==1)
        return 'Q';
    //没有玩家或者电脑赢，也没有平局，游戏继续
    return 'C';
}








